package com.example.order.controller;

import com.example.common.result.R;
import com.example.order.entity.Order;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{id}")
    public R<Order> getOrder(@PathVariable Long id) {
        return R.ok(orderService.getById(id));
    }

    @GetMapping("/no/{orderNo}")
    public R<Order> getOrderByOrderNo(@PathVariable String orderNo) {
        return R.ok(orderService.getOrderByOrderNo(orderNo));
    }

    @PostMapping("/create")
    public R<Order> createOrder(@RequestParam Long userId, @RequestParam BigDecimal amount) {
        return R.ok(orderService.createOrder(userId, amount));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Order order) {
        return R.ok(orderService.updateById(order));
    }

    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(orderService.removeById(id));
    }
}
